import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { type Options, defineConfig } from 'tsup'

const currentPath = fileURLToPath(new URL('.', import.meta.url))
const packageDir = resolve(currentPath, '../packages')

const baseConfig: Options = {
  bundle: true,
  target: 'esnext',
  dts: true,
  tsconfig: resolve(currentPath, '../tsconfig.json'),
  format: 'esm',
  platform: 'node',
  treeshake: true,
  external: ['commander', 'chalk', '@inquirer/prompts', 'joycon'],
  clean: true
}

const configMapper = (name: string) => ({
  ...baseConfig,
  entryPoints: {
    index: resolve(packageDir, `${name}/src/index.ts`)
  },
  outDir: resolve(packageDir, `${name}/dist`)
})

export default defineConfig([
  ...['shared', 'core', 'commit', 'prompts'].map(configMapper),
  {
    ...baseConfig,
    entry: {
      mojito: resolve(packageDir, 'mojito/src/index.ts')
    },
    noExternal: [/./],
    shims: true,
    outDir: resolve(packageDir, 'mojito/dist'),
    dts: false,
    banner: {
      js: `import { createRequire } from 'node:module'
       const require = createRequire(import.meta.url)`
    },
    minify: true
  }
])
